package com.hzlh.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hzlh.common.util.MD5;
import com.hzlh.core.service.impl.SystemParameterServiceImpl;
import com.hzlh.domain.po.LoginAdmin;
import com.hzlh.domain.po.SystemInitPo;
import com.hzlh.log.MethodLog;
import com.hzlh.service.LoginService;
import com.hzlh.shiro.ShiroUser;

@Controller
public class LoginController {
	@Autowired
	private LoginService loginService;
	
	@Autowired
	private SystemParameterServiceImpl systemParameterServiceImpl;

	@RequestMapping(value = { "/login", "" })
	public String login() {
		return "layout/login";
	}

	@MethodLog(key="USER.LOGIN")
	@RequestMapping(value = "/index")
	public String index(Model model) {
		Subject subject = SecurityUtils.getSubject();
		ShiroUser shiroUser = (ShiroUser)subject.getPrincipal();
		if(shiroUser == null){
			return "redirect:/login.htm";
		}
		return "layout/main";
	}

	@RequestMapping("/validLogin")
	public String validLogin(LoginAdmin user,String chknumber,String sysCode, HttpServletRequest request, Model model) {
//		user.setPwd(MD5.encode("123456"));//TODO 

		if (StringUtils.isNotBlank(user.getUserId()) && StringUtils.isNotBlank(user.getPwd())) {
        	HttpSession session = request.getSession();
        	String chknum = (String) session.getAttribute("certCode");
//        	if (!chknum.equalsIgnoreCase(chknum)) {
        	if (!chknumber.equalsIgnoreCase(chknum)) {//TODO
				model.addAttribute("msg", "验证码错误!");
				return "layout/login";
			}
        	user.setPwd(MD5.encode(user.getPwd()));//TODO
			LoginAdmin loginUser = loginService.login(user);
			if (loginUser != null) {
				UsernamePasswordToken token = new UsernamePasswordToken(loginUser.getUserId(), loginUser.getPwd());
				Subject subject = SecurityUtils.getSubject();
				//在ShiroDbRealm.java中放入shiro管理
			    subject.login(token);
				return "redirect:/index.htm";
			} else {
				model.addAttribute("msg", "用户名或密码错误，登录失败!");
				return "layout/login";
			}
        } else {
        	model.addAttribute("msg", "用户名或密码不能为空!");
			return "layout/login";
        }
	}

	@MethodLog(key="USER.LOGOUT")
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {
		SecurityUtils.getSubject().logout();
		return "layout/login";
	}
	 
	@RequestMapping("/send")
	public String send(String page,Model model) {
		if(page.endsWith("top")){
			Subject subject = SecurityUtils.getSubject();
			ShiroUser user = (ShiroUser) subject.getPrincipal();
			if(user!=null){
				SystemInitPo sysInit = user.getSysInit();
				model.addAttribute("sysInit", sysInit);
			}
		}
		
		
		return page;
	}
	
//	/**
//	 * 经分系统调用
//	 * @param page
//	 * @param request
//	 * @return
//	 */
//	@RequestMapping("/sendForBAP")
//	public String getBusAnalysisPage(String page,HttpServletRequest request){
//		String bp = ConfigPropertiesUtils.getProValue("busiAnalysis_BasePath");
//		request.setAttribute("iframeSrc", bp+"send.htm?page=" + page);
//		return "layout/hidPage";
//		
//	}
	
	
}
